package com.example.demo.service;


import com.example.demo.domain.Category;
import com.example.demo.repository.CateGoryRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CateGoryService {

    @Autowired
    private CateGoryRepository cateGoryRepository;


    public Category findOne(Integer id) {
        return cateGoryRepository.findOne(id);
    }
    public List<Category>  findAll(){return cateGoryRepository.findAll();}

    public Object getPageCateGory(int offset,int limit,String mess){
        Map<String,Object> map = new HashMap<>();
        map.put("rows",cateGoryRepository.findAllPage(offset,limit,mess));
        map.put("total", cateGoryRepository.count());
        return map;
    }

    public Object insertCategory(Category category){
        Map<String, Object> map = new HashMap<>();
        try {
           Object obj= cateGoryRepository.saveAndFlush(category);
            if(obj!=null){
                map.put("flag", 1);
            }else{
                map.put("flag", 0);
            }
        } catch (Exception e) {
            e.printStackTrace();
            map.put("flag", 0);
        }
        return map;
    }
    public Object deleteCategory(int id){
           Map<String, Object> map = new HashMap<>();
           cateGoryRepository.delete(id);;
            map.put("flag", 1);
        return map;
    }


}
